# tests/conftest.py
import pytest
import allure
import os
from datetime import datetime


@pytest.fixture(scope="session")
def allure_report_dir():
    """提供Allure报告目录"""
    return "/Users/siyiyi/automation_-interface_testing/allures"


@pytest.fixture(scope="function")
def test_logger():
    """测试日志记录"""
    with allure.step("测试初始化"):
        allure.attach("开始时间", datetime.now().strftime("%Y-%m-%d %H:%M:%S"))


@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    """获取测试结果并附加到Allure报告"""
    outcome = yield
    report = outcome.get_result()

    if report.when == "call" and report.failed:
        # 测试失败时附加额外信息
        with allure.step("测试失败分析"):
            allure.attach("失败信息", str(report.longreprtext), AttachmentType.TEXT)


def pytest_configure(config):
    """pytest配置钩子"""
    # 添加自定义标记
    config.addinivalue_line(
        "markers", "smoke: 冒烟测试"
    )
    config.addinivalue_line(
        "markers", "api: API接口测试"
    )